//
//  STAddServiceSelVC.m
//  YiYueHairdresser
//
//  Created by zhouzhouzhou on 2018/9/30.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STAddServiceSelVC.h"
#import "STSerSetVC.h"
#import "STMultiMealVC.h"
#import "STSingleMealVC.h"

@interface STAddServiceSelVC ()< UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,
UICollectionViewDelegate>{
    UICollectionView *selCollectionView;
    NSArray * arrHaircuts;
}

@end

@implementation STAddServiceSelVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"添加服务";
    [self wr_setNavBarTintColor:[UIColor blackColor]];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self loadCollectionView];
    [self loadHaircutsListData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)loadHaircutsListData{
    [STHairService getBarberConditionWithPackageType:0 completion:^(id result, NSError *error) {
        if(error){
            [ZYHCommonMethod showError:error];
        }
        arrHaircuts = [NSArray modelArrayWithClass:[STServiceCategoryModel class] json:result];
        [selCollectionView reloadData];
    }];
}

-(void)loadCollectionView{
    //    float Wall=kGTSDeviceFullWidth - 40;
    float w = (SCREEN_WIDTH-41) / 3;
    float h = w*0.36;
    
    UICollectionViewFlowLayout * flow =[[UICollectionViewFlowLayout alloc]init];
    flow.itemSize=CGSizeMake(w, h);
    flow.headerReferenceSize = CGSizeMake(SCREEN_WIDTH,40.0f);
    flow.sectionInset=UIEdgeInsetsMake(0,10,0,10);
    selCollectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-49) collectionViewLayout:flow];
    [selCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"selCollectionCell"];
    [selCollectionView registerClass:[UICollectionReusableView class]
           forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                  withReuseIdentifier:@"headerView"];
    selCollectionView.delegate = self;
    selCollectionView.dataSource = self;
    selCollectionView.scrollEnabled=NO;
    selCollectionView.showsHorizontalScrollIndicator = NO;
    selCollectionView.showsVerticalScrollIndicator = NO;
    selCollectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:selCollectionView];
}


#pragma mark -collectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section ==0) {
        return arrHaircuts.count;
    }else{
        return 2;
    }
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *selfreusableview = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *selfheaderView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                                      withReuseIdentifier:@"headerView"
                                                                                             forIndexPath:indexPath];
        selfreusableview = selfheaderView;
    }
    [selfreusableview removeAllSubviews];
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    headView.backgroundColor = [UIColor clearColor];
    UILabel * nameLab =[[UILabel alloc]initWithFrame:CGRectMake(15, (headView.frame.size.height-20)/2+10, 100, 20)];
    if(indexPath.section ==0 ){
        nameLab.text =@"单项服务";
    }else{
        nameLab.text =@"套餐服务";
    }
    
    nameLab.font = FONT(13);
    nameLab.textColor = [UIColor grayColor];
    [headView addSubview:nameLab];
    [selfreusableview addSubview:headView];
    selfreusableview.backgroundColor = [UIColor clearColor];
    
    return selfreusableview;
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

//- (CGFloat)collectionView:(UICollectionView *)collectionView
//                   layout:(UICollectionViewLayout *)collectionViewLayout
//minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
//    return 10;
//}
//
//// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"selCollectionCell";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    UIButton * typeBtn = [[UIButton alloc]initWithFrame:cell.bounds];
    [typeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    typeBtn.titleLabel.font = FONT(13);
    typeBtn.backgroundColor = [UIColor whiteColor];
    typeBtn.layer.cornerRadius = 5;
    typeBtn.layer.masksToBounds = YES;
    typeBtn.enabled = NO;
    if(indexPath.section ==0){
        STServiceCategoryModel * model = arrHaircuts[indexPath.row];
        [typeBtn setTitle:model.name forState:UIControlStateNormal];
    }else{
        if (indexPath.row==0) {
            [typeBtn setTitle:@"单项套餐" forState:UIControlStateNormal];
        }else{
            [typeBtn setTitle:@"多项套餐" forState:UIControlStateNormal];
        }
    }
    [cell.contentView addSubview:typeBtn];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell * cell = [collectionView cellForItemAtIndexPath:indexPath];
    UIButton * btn =cell.contentView.subviews.lastObject;
    btn.selected = YES;
    [btn setTitleColor:WhiteColor forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor orangeColor];
    
    STSerSetVC * newSer = [[STSerSetVC alloc]init];
    newSer.serName = btn.titleLabel.text;
    if(indexPath.section ==0){
        newSer.serType = @"Individual";
        STServiceCategoryModel * model = arrHaircuts[indexPath.row];
        newSer.serviceTypeData = model;
        STServiceInfoModel * service = [STServiceInfoModel new];
        STServiceCategoryModel * categoryModel = arrHaircuts[indexPath.row];
        service.options = categoryModel.options;
        newSer.serviceModel = service;
        newSer.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:newSer animated:NO];
    }else{
        newSer.serType = @"Package";
        newSer.serTypeB = indexPath.row;
        if (indexPath.row ==0) {
            STSingleMealVC * singleVC = [STSingleMealVC new];
            singleVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:singleVC animated:NO];
        }else{
            STMultiMealVC * multiVC = [STMultiMealVC new];
            multiVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:multiVC animated:NO];
        }
    }
    
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell * cell = [collectionView cellForItemAtIndexPath:indexPath];
    UIButton * btn =cell.contentView.subviews.lastObject;
    btn.selected = NO;
    [btn setTitleColor:BlackColor forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor whiteColor];
}
@end
